package com.soft.mainsite.module.show.web;

import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.show.entity.Show;
import com.soft.mainsite.module.show.sevice.ShowService;
import com.soft.mainsite.module.viewshow.entity.ViewShow;
import com.soft.mainsite.module.viewshow.service.ViewShowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * ViewShowController此类用于
 *
 * @author: hujm
 * @date: 2019年09月12日 11:45
 * @remark:
 */
@Slf4j
@Controller
@RequestMapping("/view/show")
public class ViewShowController {

    @Autowired
    private ViewShowService viewShowService;

    @Autowired
    private ShowService showService;

    @RequestMapping
    public String index() {
        return "viewShow/view_show";
    }

    @RequestMapping("/list")
    public String list(Model model){
        List<Show> showList = showService.findAll();
        model.addAttribute("showList", showList);
        return "viewShow/view_show_input";
    }

    @RequestMapping("/input")
    public String input(@RequestParam("categoryId")Integer categoryId, Model model){
        List<Show> viewShowList = showService.findByCategoryId(categoryId);
        model.addAttribute("viewShowList", viewShowList);
        model.addAttribute("categoryId", categoryId);
        return "viewShow/view_show_input";
    }

    @RequestMapping("/save")
    @ResponseBody
    public String save(@RequestParam("showIds")List<Integer> showIds, @RequestParam("categoryId")Integer categoryId){
        ViewShow viewShow = viewShowService.selectShowIds(categoryId);
        if (viewShow == null){
            showService.save(showIds, categoryId);
            return JsonUtil.toSuccess("新增设置成功！", "view-show-tab", false);
        }
        int result = showService.update(showIds, categoryId);
        if (result < 1){
            return JsonUtil.toError("修改失败！");
        }
        return JsonUtil.toSuccess("保存设置成功！", "view-show-tab", false);
    }
}
